package com.zd.commodity.productMng.action;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import utils.Util;

import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.productMng.biz.INameProductRuleBiz;
import com.zd.commodity.productMng.biz.IProductAttrBiz;
import com.zd.commodity.productMng.entity.NameProductRule;
import com.zd.commodity.productMng.entity.ProductAttr;

@Scope("prototype")
@Controller("nameProductRuleAction")
public class NameProductRuleAction extends BaseAction<NameProductRule> {

	private INameProductRuleBiz nameProductRuleBiz;

	public INameProductRuleBiz getNameProductRuleBiz() {
		return nameProductRuleBiz;
	}
    @Autowired
	public void setNameProductRuleBiz(INameProductRuleBiz nameProductRuleBiz) {
		this.nameProductRuleBiz = nameProductRuleBiz;
	    this.setBaseBiz(nameProductRuleBiz);
	}
    
    @Autowired
    private IProductAttrBiz productAttrBiz;
    @Override
    public String get()
	{		
    	NameProductRule nameProductRule = nameProductRuleBiz.get(super.getId());
    	
    	//拼接原有的模板信息  进行显示
    	String attrIds=nameProductRule.getPartakeNameAttr();
    	String oldTemplate="";
    	if(attrIds!=null && !attrIds.equals("")){
    		String[] attrs = attrIds.split(",");
        	HashMap<String, String> map=new HashMap<String, String>();
        	for (String attr : attrs) {
        		ProductAttr productAttr=productAttrBiz.get(Long.valueOf(attr));
        		map.put(attr, " [ "+productAttr.getName()+" ] ");
    		}
        	
        	oldTemplate=nameProductRule.getTemplate();
        	oldTemplate = Util.replacWithMap(oldTemplate, "{:", ":}", map);
    	}
    	ActionContext.getContext().put("oldTemplate", oldTemplate);
    	
    	
		request.setAttribute("page", super.getPage());
		request.setAttribute("params", super.getParams());
		ActionContext.getContext().put("t", nameProductRule);
		ActionContext.getContext().put("method", "update");
		return "form";
	}
    
    @Override
	public String add()
	{
		try {
			NameProductRule t=super.getT();
			if(t.getProductAttr()!=null){
	    		if(t.getProductAttr().getId()==null || t.getProductAttr().getId().equals("")){
	    			t.setProductAttr(null);
	    		}
	    	}
			nameProductRuleBiz.add(t);			
		} catch (Exception e) {			
			e.printStackTrace();
		}
		
		return listByPage();
	}
    
    @Override
    public String update()
	{
		try {
			NameProductRule nameProductRule=super.getT();
			String tt = nameProductRule.getTemplate();
			//模板为空不修改
			if(!tt.equals("") && tt!=null){
				if(nameProductRule.getProductAttr()!=null){
		    		if(nameProductRule.getProductAttr().getId()==null || nameProductRule.getProductAttr().getId().equals("")){
		    			nameProductRule.setProductAttr(null);
		    		}
		    	}
				nameProductRuleBiz.update(nameProductRule);	
			}
			request.setAttribute("page", super.getPage());
		} catch (Exception e) {			
		
		}
		return listByPage();
	}
	
}
